# -*- coding: utf-8 -*-

# 抓鸭子游戏 - 炸弹捕手

import sys, pygame, random, time
from pygame.locals import *

reload(sys)
sys.setdefaultencoding('utf-8')

def print_text(font, x, y, text, color=(255,255,255), shadow=False):
    if shadow:
        imgText = font.render(text, True, (0, 0, 0))
        screen.blit(imgText, (x - 2, y - 2))
    image = font.render(text, True, color)
    screen.blit(image, (x, y))

white = 255,255,255
purple = 255,0,255
yellow = 255,255,0
red = 255,0,0
black = 0,0,0

pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("抓鸭子游戏")
font1 = pygame.font.Font("mini.ttf", 20)

mouse_x = mouse_y = 0
lives = 3
score = 0
game_over = True
pos_x = 300
pos_y = 460
bomb_x = random.randint(0, 500)
bomb_y = -50
vel_y = 0.5

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == MOUSEMOTION:
            mouse_x,mouse_y = event.pos
        elif event.type == MOUSEBUTTONUP:
            if game_over:
                game_over = False
                lives = 3
                score = 0

    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()

    screen.fill((0, 0, 100))

    if game_over:
        print_text(font1, 270, 230, u"点击开始")
    else:
        bomb_y += vel_y
        if bomb_y > 500:
            bomb_x = random.randint(0, 500)
            bomb_y = -50
            lives -= 1
            if lives == 0:
                game_over = True

        elif bomb_y > pos_y:
            if bomb_x > pos_x and bomb_x < pos_x + 120:
                score += 1
                bomb_x = random.randint(0, 500)
                bomb_y = -50

        # bomb
        pygame.draw.circle(screen, black, (bomb_x-4,int(bomb_y)-4), 30, 0)
        pygame.draw.circle(screen, yellow, (bomb_x,int(bomb_y)), 30, 0)

        pos_x = mouse_x
        if pos_x < 0:
            pos_x = 0;
        elif pos_x > 500:
            pos_x = 500

        # player
        pygame.draw.rect(screen, black, (pos_x-4, pos_y-4, 120, 40), 0)
        pygame.draw.rect(screen, red, (pos_x, pos_y, 120, 40), 0)

    print_text(font1, 0, 0, u"生命：" + str(lives))
    print_text(font1, 500, 0, u"分数：" + str(score))

    pygame.display.update()


